//
//  WNFeedBackVC.swift
//  WalkNovel
//
//  Created by Kevin on 2023/11/23.
//

import UIKit

class WNFeedBackVC: WNBaseVC {
    
    lazy var feedbackLabel: UILabel = {
        let label = UILabel()
        label.text = "If you have any usage problems or feature suggestions, please feel free to submit them to us. We will carefully refer to them and optimize and modify them."
        label.textColor = .init(named: "black_333333")
        label.font = .wn_font(name: .regular, size: 14)
        label.numberOfLines = 0
        return label
    }()

    lazy var textView: IQTextView = {
        let view = IQTextView()
        view.textContainerInset = .init(top: 10, left: 10, bottom: 10, right: 10)
        view.placeholder = "Add your feedback..."
        view.font = .wn_font(name: .regular, size: 14)
        view.tintColor = .init(named: "red_FE95B4")
        view.textColor = .init(named: "black_333333")
        view.backgroundColor = .init(named: "white_F7F7F7")
        view.returnKeyType = .done
        view.keyboardDismissMode = .none
        view.textContainer.lineFragmentPadding = 10
        view.rx.didChange.subscribe(onNext: { [weak self] in
            if view.text.count > WNRegex.FeedbackMaxLen {
                self?.wn_toast("\(NSLocalizedString("Max of write in ", comment: ""))\(WNRegex.FeedbackMaxLen)\(NSLocalizedString(" Words", comment: ""))")
                view.text = String(view.text.prefix(WNRegex.FeedbackMaxLen))
            }
            self?.handleTag()
        }).disposed(by: rx.disposeBag)
        view.wn_cornerWithBorder(8)
        return view
    }()

    lazy var submitBtn: UIButton = {
        let btn = UIButton(type: .custom)
        btn.setTitle("Submit", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.titleLabel?.font = .wn_font(name: .bold, size: 16)
        btn.rx.tap.bind { [weak self] in
            self?.feedbackRequest()
        }.disposed(by: rx.disposeBag)
        btn.backgroundColor = WNColor.Fade().left2right(.init(width: scaleWidth(240), height: 44))
        btn.wn_cornerWithBorder(22)
        return btn
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewConfig() {
        view.addSubview(feedbackLabel)
        view.addSubview(submitBtn)
        view.addSubview(textView)
        feedbackLabel.snp.makeConstraints { make in
            make.top.equalTo(NaviTotalHeight+20)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.height.equalTo(80)
        }
        submitBtn.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-TabBarMargin-42)
            make.width.equalTo(scaleWidth(240))
            make.height.equalTo(44)
        }
        textView.snp.makeConstraints { make in
            make.top.equalTo(feedbackLabel.snp.bottom).offset(30)
            make.left.equalTo(12)
            make.right.equalTo(-12)
            make.bottom.equalTo(submitBtn.snp.top).offset(-30)
        }
    }
    
    override func dataConfig() {
        pageName = "FeedBack"
        navView.titleName = "FeedBack"
        handleTag()
    }
    
    func handleTag() {
        let count = textView.text.count
        submitBtn.isEnabled = count > 0
    }
    
    func feedbackRequest() {
        guard textView.text.count > 0 else {
            wn_toast("Fill in at least 1 word")
            return
        }
        wn_toast("Thank you for your valuable advice!")
        WNBuriedManager.shared.contact()
        WNThread.wn_delay(2) {
            self.navigationController?.popViewController(animated: true)
        }
    }
}
